package com.haw.app.db;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;

import com.haw.app.parser.Veranstaltung;

public class DbHAW {

	private SQLiteDatabase db;
	private static DbHAW self = null;

	public static DbHAW getInstance(Context context) {
		return self == null ? self = new DbHAW(context) : self;
	}

	// Constructor for DbHAW
	private DbHAW(Context context) {
		DbOpenHelper dbOpenHelper = new DbOpenHelper(context);
		this.db = dbOpenHelper.getWritableDatabase();
	}

	// Make sure to call this method before creating an instance of DbHAW
	public static boolean doesDBexist() {
//		SQLiteDatabase checkDB = null;
//		try {
//			checkDB = SQLiteDatabase.openDatabase(DbOpenHelper.DB_NAME, null,
//					SQLiteDatabase.OPEN_READONLY);
//			checkDB.close();
//		} catch (SQLiteException e) {
//			return false;
//		}
//		return true;
		File dbFile = new File(DbOpenHelper.DB_PATH + DbOpenHelper.DB_NAME);
		boolean b = dbFile.exists(); 
	    return b;
	}

	public void deleteDB() {
		
		File dbFile = new File(DbOpenHelper.DB_PATH + DbOpenHelper.DB_NAME);
		if (dbFile.exists() == true) {
			dbFile.delete();
		}
	}
	
	public void addTerminplanAttribute(String attrName, String attrValue) {

		ContentValues cv = new ContentValues();
		cv.put(DbOpenHelper.NAME, attrName);
		cv.put(DbOpenHelper.VALUE, attrValue);
		db.insert(DbOpenHelper.TERMINPLAN_ATTR_TABLE_NAME, null, cv);
	}

	// Adds Veranstaltung object to database
	public void addVeranstaltung(Veranstaltung v) {

		ContentValues cv = new ContentValues();

		cv.put(DbOpenHelper.NAME, v.getName());
		cv.put(DbOpenHelper.DOZENT, v.getDozent());
		cv.put(DbOpenHelper.RAUM, v.getRaum());
		cv.put(DbOpenHelper.WOCHENTAG, v.getWochentag());
		cv.put(DbOpenHelper.ANFANG, v.getAnfangsZeit());
		cv.put(DbOpenHelper.ENDE, v.getEndeZeit());
		cv.put(DbOpenHelper.KW, v.getKWs());
		cv.put(DbOpenHelper.NOTITZ, v.getNotitz());

		db.insert(DbOpenHelper.VERANSTALTUNGEN_TABLE_NAME, null, cv);
	}

	// Closes database
	public void closeDB() {
		this.db.close();
	}
	
	private Cursor getVeranstaltungCursor(int kw) {
		return db.query(DbOpenHelper.VERANSTALTUNGEN_TABLE_NAME, null, "kw = "
				+ kw, null, null, null, null);
	}

	private Cursor getAllVeranstaltungCursor() {
		return db.query(DbOpenHelper.VERANSTALTUNGEN_TABLE_NAME, null, null,
				null, null, null, null);
	}

	private Cursor getTerminplanAttributeCursor(String attrName) {

		Cursor c = null;
		try {
			c = db.query(DbOpenHelper.TERMINPLAN_ATTR_TABLE_NAME, null,
					"name = \"" + attrName + "\"", null, null, null, null);
		} catch (Exception e) {
			Log.e("HAWApp", e.getMessage());
		}
		return c;
	}

	public List<Veranstaltung> getAllVeranstaltungList() {
		Cursor c = this.getAllVeranstaltungCursor();

		List<Veranstaltung> vList = new ArrayList<Veranstaltung>();

		if (true == c.moveToFirst()) {
			do {
				vList.add(new Veranstaltung(c.getString(c
						.getColumnIndex(DbOpenHelper.NAME)), c.getString(c
						.getColumnIndex(DbOpenHelper.DOZENT)), c.getString(c
						.getColumnIndex(DbOpenHelper.RAUM)), c.getString(c
						.getColumnIndex(DbOpenHelper.WOCHENTAG)), c.getString(c
						.getColumnIndex(DbOpenHelper.ANFANG)), c.getString(c
						.getColumnIndex(DbOpenHelper.ENDE)), c.getString(c
						.getColumnIndex(DbOpenHelper.KW)), c.getString(c
						.getColumnIndex(DbOpenHelper.NOTITZ))));
			} while (true == c.moveToNext());
		}
		c.close();

		return vList;
	}

	public String getTerminplanAttribute(String attrName) {

		Cursor c = this.getTerminplanAttributeCursor(attrName);
		String attr = "";
		if (null != c && true == c.moveToFirst()) {
			attr = c.getString(c.getColumnIndex(DbOpenHelper.VALUE));
			c.close();
		}
		return attr;
	}

	public void deleteAllVeranstaltungen() {
		db.delete(DbOpenHelper.VERANSTALTUNGEN_TABLE_NAME, null, null);
		db.delete(DbOpenHelper.TERMINPLAN_ATTR_TABLE_NAME, null, null);
	}

}// Class
